package utils

import (
	"math/rand/v2"
	"strings"
	"time"

	"gitee.com/ameise84/e3utils/uuid"
)

var _gRand *rand.Rand

func init() {
	seed := uint64(time.Now().Unix())
	_gRand = rand.New(rand.NewPCG(seed, seed))
}

func RandSeed(s1 uint64, s2 uint64) {
	_gRand = rand.New(rand.NewPCG(s1, s2))
}

func Rand1K() int {
	return _gRand.IntN(1000)
}

func Rand10K() int {
	return _gRand.IntN(10000)
}

func Rand100K() int {
	return _gRand.IntN(1000)
}

func RandIntN(max int) int {
	return _gRand.IntN(max)
}

func RandInt32N(max int32) int32 {
	return _gRand.Int32N(max)
}

func RandInt64N(max int64) int64 {
	return _gRand.Int64N(max)
}

func Shuffle(n int, swap func(i, j int)) {
	_gRand.Shuffle(n, swap)
}

func RandString(n int) string {
	sb := strings.Builder{}
	for {
		s := uuid.NewShortV4()
		if len(s) < n {
			sb.WriteString(s)
			n -= len(s)
		} else {
			sb.WriteString(s[:n])
			break
		}
	}
	return sb.String()
}

func ShuffleSlice[T any](s []T) {
	_gRand.Shuffle(len(s), func(i, j int) {
		s[i], s[j] = s[j], s[i]
	})
}

func RandInSlice[T any](s []T) T {
	size := len(s)
	if size == 0 {
		panic("rand in slice ,slice is empty")
	}
	n := _gRand.IntN(size)
	return s[n]
}
